package com.solution.admin.product.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.solution.admin.customer.entity.CustomerCustomFieldEntity;
import com.solution.admin.customer.entity.CustomerLevelEntity;
import com.solution.admin.customer.service.CustomerCustomFieldService;
import com.solution.admin.customer.service.CustomerLevelService;
import com.solution.admin.customer.service.CustomerTypeService;
import com.solution.admin.product.entity.ProductInvisiableEntity;
import com.solution.admin.product.service.ProductInvisiableService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;

/**
 * 商品不可见性web控制层
 * 
 * @author solin
 * @date 2017-09-20 15:31:21
 */
@RequestMapping("/productInvisiable")
@Controller
public class ProductInvisiableController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(ProductInvisiableController.class);

	@Autowired
	private ProductInvisiableService productInvisiableService;
	@Autowired
	private CustomerTypeService customerTypeService;
	@Autowired
	private CustomerCustomFieldService customerCustomFieldService;
	@Autowired
	private CustomerLevelService customerLevelService;

	/**
	 * 获取已选择的商品不可见性
	 * 
	 * @param ebProductInvisiable
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年12月20日 下午11:56:25
	 */
	@RequestMapping("/listProductInvisiable")
	@ResponseBody
	public List<ProductInvisiableEntity> listProductInvisiable(ProductInvisiableEntity ebProductInvisiable, HttpServletRequest request) {
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("invisiableType", ebProductInvisiable.getInvisiableType());
			paramMap.put("productId", ebProductInvisiable.getProductId());

			return productInvisiableService.listEbProductInvisiable(paramMap);
		} catch (Exception e) {
			handleException("分页查询商品不可见性失败", logger, e);
		}
		return new ArrayList<ProductInvisiableEntity>();
	}

	/**
	 * 获取所有可选择的客户属性
	 * 
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年12月21日 上午12:15:36
	 */
	@RequestMapping("/getAllCustomerPropertyList")
	@ResponseBody
	public List<ProductInvisiableEntity> getAllCustomerPropertyList(HttpServletRequest request, Long productId) {
		List<ProductInvisiableEntity> productInvisiableList = new ArrayList<ProductInvisiableEntity>();
		try {
			// 获取客户级别列表数据
			Map<String, Object> paramMap0 = new HashMap<String, Object>();
			List<CustomerLevelEntity> customerLevelList = customerLevelService.listEbCustomerLevel(paramMap0);
			if (null != customerLevelList && customerLevelList.size() > 0) {
				for (CustomerLevelEntity obj : customerLevelList) {
					ProductInvisiableEntity piEntity = new ProductInvisiableEntity();
					piEntity.setProductId(productId);
					piEntity.setInvisiableType("1");
					piEntity.setPropertyId(obj.getId());
					piEntity.setPropertyValue(obj.getLevelName());
					piEntity.setPropertyTypeName("客户级别");
					productInvisiableList.add(piEntity);
				}
			}
			// 获取客户自定义属性
			Map<String, Object> paramMap1 = new HashMap<String, Object>();
			paramMap1.put("status", 1);
			List<CustomerCustomFieldEntity> customFieldlist = customerCustomFieldService.listCustomerCustomField(paramMap1);
			if (null != customFieldlist && customFieldlist.size() > 0) {
				for (CustomerCustomFieldEntity obj : customFieldlist) {
					String options = obj.getChoiceOption();
					JSONArray optionArray = JSONArray.parseArray(options);
					for (int i = 0; i < optionArray.size(); i++) {
						ProductInvisiableEntity piEntity = new ProductInvisiableEntity();
						piEntity.setProductId(productId);
						piEntity.setInvisiableType("2");
						piEntity.setPropertyId(obj.getId());
						piEntity.setPropertyValue(optionArray.getJSONObject(i).getString("value"));
						piEntity.setPropertyTypeName(obj.getFieldName());
						productInvisiableList.add(piEntity);
					}
				}
			}

		} catch (Exception e) {
			handleException("分页查询商品不可见性失败", logger, e);
		}
		return productInvisiableList;
	}

	/**
	 * 分页查询商品不可见性列表
	 * 
	 * @param ebProductInvisiable
	 *            接收界面查询条件参数
	 * @param request
	 *            请求对象
	 * @return 返回对应的页面视图
	 * @author solin
	 * @date 2017-09-20 15:31:21
	 */
	@RequestMapping("/listProductInvisiablePage")
	@ResponseBody
	public JSONGrid listEbProductInvisiablePage(ProductInvisiableEntity ebProductInvisiable, HttpServletRequest request) {
		int pageNum = 1;
		int numPerPage = 10000;

		if (null != request.getParameter("page")) {
			pageNum = Integer.parseInt(request.getParameter("page").toString());
			numPerPage = Integer.parseInt(request.getParameter("rows").toString());
		}
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);

//		List<Map<String, Object>> resultMapList = new ArrayList<Map<String, Object>>();
//		int totalCount = 0;
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			Long productId = ebProductInvisiable.getProductId();
			PageBean pageBean = null;
			if (null != productId) {
				pageBean = productInvisiableService.listEbProductInvisiablePage(pageParam, paramMap);
			}
			return new JSONGrid(pageBean.getRecordList(), pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询商品不可见性失败", logger, e);
		}
		return new JSONGrid();
	}

	/**
	 * 新增或保存商品不可见性
	 * 
	 * @param request
	 *            请求对象
	 * @param ebProductInvisiableEntity
	 *            商品不可见性实体
	 * @return
	 * @author solin
	 * @date 2017-09-20 15:31:21
	 */
	@RequestMapping("/saveOrUpdateProductInvisiable")
	@ResponseBody
	public Map<String, Object> saveEbProductInvisiable(HttpServletRequest request, Long productId, String customerPropertysJsonStr) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			List<ProductInvisiableEntity> customerPropertyList = JSONArray.parseArray(customerPropertysJsonStr, ProductInvisiableEntity.class);
			for (int i = 0; i < customerPropertyList.size(); i++) {
				customerPropertyList.get(i).setCreater(this.getUserName());
			}
			result = (int) productInvisiableService.batchSaveProductInvisiable(productId, customerPropertyList);
		} catch (Exception e) {
			handleException("添加商品不可见性失败", logger, e);
		}
		resultMap.put("code", result > 0 ? 1 : 0);
		resultMap.put("msg", result > 0 ? "保存成功" : "保存失败");
		return resultMap;
	}

}
